package stark.app.function.netusage;

import android.content.Intent;

import com.lxj.xpopup.interfaces.OnConfirmListener;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import stark.app.R;
import stark.app.databinding.ActivityNetUsageDemoBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.DialogUtil;
import stark.common.basic.utils.PermissionUtil;

/**
 * Author：daizhongbin on 2022/8/10
 * Email：1027154338@qq.com
 * Desc:
 */
public class NetUsageDemoActivity extends BaseNoModelActivity<ActivityNetUsageDemoBinding> {

    private String[] titles = new String[]{"今天", "最近"};
    private static final int REQ_USAGE_STATS = 1;

    private NetUsageTodayFragment mNetUsageTodayFragment;
    private NetUsageRecentFragment mNetUsageRecentFragment;

    @Override
    protected int onCreate() {
        return R.layout.activity_net_usage_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab());
        mDataBinding.tabLayout.addTab(mDataBinding.tabLayout.newTab());
        mDataBinding.tabLayout.setupWithViewPager(mDataBinding.viewPager);
    }

    @Override
    protected void initData() {
        checkPermission();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQ_USAGE_STATS) {
            checkPermission();
            if (PermissionUtil.hasUsageStatsPermission()) {
                mNetUsageTodayFragment.updateData();
                mNetUsageRecentFragment.updateData();
            }
        }
    }

    private void checkPermission() {
        boolean has = PermissionUtil.hasUsageStatsPermission();
        if (!has) {
            DialogUtil.asConfirmNotCancel(this, "温馨提示", "为获取流量信息，您需要在系统设置中找到该应用并授权",
                    "确定",
                    new OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            PermissionUtil.reqUsageStatsPermission(NetUsageDemoActivity.this, REQ_USAGE_STATS);
                        }
                    })
                    .show();
            return;
        }
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {

        public MyPagerAdapter(@NonNull FragmentManager fm) {
            super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            if (position == 0) {
                mNetUsageTodayFragment = new NetUsageTodayFragment();
                fragment = mNetUsageTodayFragment;
            } else {
                mNetUsageRecentFragment = new NetUsageRecentFragment();
                fragment = mNetUsageRecentFragment;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }
    }

}
